<?php defined('SYSPATH') OR die('No direct script access.');

class Database_Query_Builder_Select extends Kohana_Database_Query_Builder_Select {

	protected $for_update = FALSE;

	public function for_update()
	{
		$this->for_update = TRUE;
		return $this;
	}

	public function reset($condition = NULL)
	{
		(empty($condition) || $condition == 'select') && $this->_select   = array();
		(empty($condition) || $condition == 'from') && $this->_from     = array();
		(empty($condition) || $condition == 'join') && $this->_join     = array();
		(empty($condition) || $condition == 'where') && $this->_where    = array();
		(empty($condition) || $condition == 'group_by') && $this->_group_by = array();
		(empty($condition) || $condition == 'having') && $this->_having   = array();
		(empty($condition) || $condition == 'order_by') && $this->_order_by = array();
		(empty($condition) || $condition == 'union') && $this->_union = array();

		(empty($condition) || $condition == 'distinct') && $this->_distinct = FALSE;

		(empty($condition) || $condition == 'limit') && $this->_limit     = NULL;
		(empty($condition) || $condition == 'offset') && $this->_offset    = NULL;
		(empty($condition) || $condition == 'join') && $this->_last_join = NULL;
		(empty($condition) || $condition == 'for update') && $this->for_update = NULL;

		empty($condition) && $this->_parameters = array();

		empty($condition) && $this->_sql = NULL;

		return $this;
	}

	public function compile($db = NULL)
	{
		return $this->_sql = parent::compile($db) . ($this->for_update ? ' FOR UPDATE' : '');
	}
}
